package com.smart.service.impl;

import java.util.Map;
import java.util.List;
import com.smart.vo.SsWorkVo;
import com.smart.vo.AppSSCarVO;
import com.smart.util.DateUtil;
import com.smart.entity.SsPile;
import com.smart.entity.SsWork;
import com.smart.entity.SysUser;
import java.time.LocalDateTime;
import com.smart.util.Pageable;
import com.smart.util.SysUserUtils;

import javax.annotation.Resource;
import com.smart.mapper.CarMapper;
import com.smart.mapper.SsWorkMapper;
import cn.hutool.core.util.StrUtil;
import com.smart.service.CarService;
import com.smart.mapper.SyPileMapper;
import com.smart.service.SsWorkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月11日 下午12:00:24
 * Copyright (C) 深圳工勘科技
 * @说明: 作业service
 **/
@Service
public class SsWorkServiceImpl extends BaseServiceImpl<SsWork,Long>
       implements SsWorkService{

	@Resource
	private SyPileMapper pileMapper;
	@Resource
	private CarMapper carMapper;
	@Resource
	private CarService carService;
	@Resource
	private SsWorkMapper ssWorkMapper;
	/**
	 * (non-Javadoc)
	 * @see 作业service
	 **/
	@Override
	@Transactional(readOnly=true)
	public com.smart.util.Page<SsWork> findPage(Pageable pageable) {
		LambdaQueryWrapper<SsWork> wrapper=new LambdaQueryWrapper<>();
		String carno = pageable.getSearch().get("carno");
		String pileno= pageable.getSearch().get("pileno");
		String name  = pageable.getSearch().get("name");
		String workTime_s = pageable.getSearch().get("workTime_start");
		String workTime_e = pageable.getSearch().get("workTime_end");
		if(StringUtils.isNotEmpty(carno)){
			wrapper.like(SsWork::getCarNo,carno);
		}
		if(StringUtils.isNotEmpty(pileno)){
			wrapper.like(SsWork::getPileNo,pileno);
		}
		if(StringUtils.isNotEmpty(name)){
			wrapper.like(SsWork::getName,name);
		}
		if(StringUtils.isNoneEmpty(workTime_s)){
			wrapper.ge(SsWork::getWorkTime,workTime_s);
		}
		if(StringUtils.isNotEmpty(workTime_e)){
			wrapper.le(SsWork::getWorkTime,workTime_e);
		}
		return super.findPage(pageable,wrapper);
	}

	@Override
	public String saveSsWork(SsWorkVo dto) {
        //获取当前用户id
		SysUser userInfo = SysUserUtils.getSysUser();
        Long userId = userInfo.getId();
        Map<String,Object> car=carService.getCarListByUserId(userId);
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        String createTime = DateUtil.format(localDateTime, "yyyy-MM-dd HH:mm:ss");
        //先存桩编号 返回id  存入作业
        SsPile appSsPile = new SsPile();
        appSsPile.setPileNo(dto.getPileNo());
        pileMapper.insert(appSsPile);
//        //查询车辆信息并设置值
        AppSSCarVO vo = carMapper.getSsCar(userId);
        //通过构造参数设置前端传过来的值
        SsWork appSsWork = new SsWork(vo,appSsPile,createTime,userId);
        //处理多张图片,逗号拼接
        if (!CollectionUtils.isEmpty(dto.getPicUrls())) {
            appSsWork.setPicUrl(String.join(",", dto.getPicUrls()));
        }
        //copy值到实体
        BeanUtils.copyProperties(dto, appSsWork);
        appSsWork.setCarType(car.get("dict_label").toString());
        appSsWork.setNo(System.currentTimeMillis()+"");
        appSsWork.setName(userInfo.getNickName());
        int insert = baseMapper.insert(appSsWork);
        if (insert > 0) {
            //由于APP端显示新增时间的奇葩需求,所以返回新增时间
            return createTime;
        }
        return createTime;
    }

	@Override
	@SuppressWarnings("unchecked")
	@Transactional(readOnly=true)
	public List<SsWork> findPage(String createTime, Integer pageNum, Integer pageSize) {
		Long id = SysUserUtils.getSysUser().getId();
        LambdaQueryWrapper<SsWork> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SsWork::getUserId,id);
        if (StrUtil.isNotEmpty(createTime)) {
            wrapper.gt(SsWork::getCreateTime,createTime+" 00:00:00");
            wrapper.le(SsWork::getCreateTime,createTime+" 23:59:59");
        }
        wrapper.orderByDesc(SsWork::getCreateTime);
        IPage<SsWork> i=page(new Page<SsWork>(pageNum,pageSize),wrapper);
        return i.getRecords();
	}

	/**
	 * @param type 1:当日2:当周3:当月
	 * @return 作业进度统计
	 **/
	@Override
	@Transactional(readOnly=true)
	public Integer[] statisticalSsWork(int type) {
		int total = ssWorkMapper.findWorkCount(type);
		int onTotal=ssWorkMapper.findWorkOnCount(type);
		Integer[] res ={onTotal,(total-onTotal)};
		return res;
	}

}